//
//  NovelCategoryView.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-5-27.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "NovelCategoryView.h"
#import "NovelCell.h"
#import "NovelInfoViewController.h"
#import "AppDelegate.h"

@interface NovelCategoryView ()
@property (nonatomic,retain) NSArray *novelArray;
@end

@implementation NovelCategoryView

- (void)awakeFromNib
{
    [super awakeFromNib];
    novelGridView.requiresSelection = NO;
    novelGridView.layoutDirection = AQGridViewLayoutDirectionHorizontal;//水平显示.
    novelGridView.showsHorizontalScrollIndicator = NO;
}

- (void)setCategory:(NSDictionary *)category
{
    if (category && ![category isEqualToDictionary:_category]) {
        [_category release];
        _category = [category retain];
        [self resetDisplay];
        [self getData];
    }
}


- (void)getData
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *data = [[DataRequest sharedRequest] getNovelByCategory:[_category objectForKey:@"class_id"] count:12];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (data && [data isKindOfClass:[NSDictionary class]] && [[data objectForKey:@"result"] isKindOfClass:[NSDictionary class]]) {
                self.novelArray = [[data objectForKey:@"result"] objectForKey:@"novel_list"];
                [novelGridView reloadData];
                [self scrollViewDidEndDecelerating:novelGridView];
            }
        });
    });
}

- (void)resetDisplay
{
    self.novelArray = nil;
    [novelGridView reloadData];
    leftArrow.hidden = YES;
    rightArrow.hidden = YES;
    nameLabel.text = [_category objectForKey:@"class_name"];
}

- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
{
    return _novelArray.count;
}

- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
{
    static NSString *CellIdentify = @"NovelCell";
    NovelCell *cell = (NovelCell *)[gridView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[NovelCell alloc] initWithFrame:CGRectMake(0, 0, 70, 100) reuseIdentifier:CellIdentify];
    }
    cell.novelInfo = [_novelArray objectAtIndex:index];
    [cell updateDisplayWithImageStr:@"cover_image"] ;
    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
    return CGSizeMake(94, 100);
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
    NovelInfoViewController * novelInfoViewController = [[[NovelInfoViewController alloc] initWithNibName:@"NovelInfoViewController" bundle:nil] autorelease];
    novelInfoViewController.basicInfo = [_novelArray objectAtIndex:index];
    [[UIApplication rootNavController] pushViewController:novelInfoViewController animated:YES];
    [gridView deselectItemAtIndex:index animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    leftArrow.hidden = (scrollView.contentOffset.x > 0) ? NO :YES;
    rightArrow.hidden = (scrollView.contentOffset.x < scrollView.contentSize.width - scrollView.bounds.size.width) ? NO :YES;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate == NO) {
        [self scrollViewDidEndDecelerating:scrollView];
    }
}

- (void)dealloc
{
    [_category release];
    [nameLabel release];
    [novelGridView release];
    [_novelArray release];
    [leftArrow release];
    [rightArrow release];
    [super dealloc];
}

@end
